package twinb.text;

import java.util.Collections;
import java.util.List;

import twinb.Conversation;

public class Search
{
	static	double		factor=100.0;
	
    TermVector        vec    = null;

    private Regex      _regex = new Regex();

    private Kgram      _kgram = new Kgram();

    List<Conversation> cvses  = null;

    public Search(List<Conversation> cvses)
    {
        this.cvses = cvses;
    }
    
    public void query(String text)
    {
    	this.setInput(text);
    	this.doSearch();
    }

    public void setInput(String text)
    {
        List<String> words = _regex.getWords(text);
        List<String> kgrams = _kgram.getKgrams(words);
        vec = new TermVector();
        vec.setSearchSpace(kgrams);
    }

    public void doSearch()
    {
        for (int i = 0; i < this.cvses.size(); ++i)
        {
            Conversation conv = this.cvses.get(i);
            //conv.reset(); // why reset when searching? -- quyin
            /*
            Yes, we donot need this right now, cause we reset that when computing the rank score,
            The reason I do this is for that I may not update the vector of conv. when a tweets coming in.
            So I update it when doing the search. Well, I will try to find a better way.
            */
            double score=this.vec.cos(conv.getVector())*Search.factor;
            conv.setSearchScore(score);
            conv.setRankScore();
        }
        Collections.sort(this.cvses);
    }
}
